package com.xiaocikuai.smallchunks.Task;

import com.xiaocikuai.smallchunks.entity.Transaction.Order;
import com.xiaocikuai.smallchunks.entity.User.Duser;
import com.xiaocikuai.smallchunks.mapper.Transaction.OrderMapper;
import com.xiaocikuai.smallchunks.service.Transaction.impl.OrderServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class TransactionTask {

    @Resource
    private OrderMapper orderMapper;



    /*    *
     * 处理支付超时订单
     */
//    @Scheduled(cron = "0 * * * * *")
    @Scheduled(cron = "0 0 * * * *")//每小时处理一次
    public void processTimeoutOrder() {

        LocalDateTime time = LocalDateTime.now().plusHours(24);
        LocalDateTime times = LocalDateTime.now().plusDays(3);
        // select * from orders where status = 1 and order_time < 当前时间-24小时
        List<Order> ordersList = orderMapper.getByStatusAndOrdertimeLT(0 , time);
        List<Order> orders =  orderMapper.getByStatusAndOrdertimeLT(-1 , time);

        if(ordersList != null && ordersList.size() > 0){
            ordersList.forEach(order -> {
                orderMapper.updateOrderDetailStatus(order);
            });
        }
        if(orders != null && orders.size() > 0){
            orders.forEach(order -> {
                orderMapper.deleteById(order);
            });
        }
    }

//    @Scheduled(cron = "* * * * * *")//每小时处理一次
//    public void deleteTimeoutOrder() {
//
//        LocalDateTime time = LocalDateTime.now().plusHours(24);
//
//        // select * from orders where status = 1 and order_time < 当前时间-24小时
//        List<Order> ordersList = orderMapper.getByStatusAndOrdertimeLT(-1 ,  LocalDateTime.now());
//        if(ordersList != null && ordersList.size() > 0){
//            ordersList.forEach(order -> {
//                orderMapper.deleteById(order);
//            });
//        }
//    }


}
